namespace TSL.Utility.Json
{
    using System;
    using System.Globalization;
    using System.Text;
    using System.Text.RegularExpressions;

    public class DateTimeConverter : IJavaScriptConverter
    {
        private Regex r = new Regex(@"(\d{4}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,3})", RegexOptions.Compiled);
        private double UtcOffsetMinutes = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;

        public DateTimeConverter()
        {
            base.m_serializableTypes = new Type[] { typeof(DateTime) };
            base.m_deserializableTypes = new Type[] { typeof(DateTime) };
        }

        public override object Deserialize(IJavaScriptObject o, Type t)
        {
            DateTime time5;
            JavaScriptObject obj2 = o as JavaScriptObject;
            if (o is JavaScriptSource)
            {
                string str = o.ToString();
                if (str.StartsWith("new Date(Date.UTC(") && str.EndsWith("))"))
                {
                    string[] strArray = str.Substring(0x12, str.Length - 20).Split(new char[] { ',' });
                    time5 = new DateTime(int.Parse(strArray[0]), int.Parse(strArray[1]) + 1, int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]), int.Parse(strArray[5]), int.Parse(strArray[6]));
                    return time5.AddMinutes(this.UtcOffsetMinutes);
                }
                if (str.StartsWith("new Date(") && str.EndsWith(")"))
                {
                    long ticks = long.Parse(str.Substring(9, str.Length - 10)) * 0x2710L;
                    time5 = new DateTime(0x7b2, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                    ticks += time5.Ticks;
                    DateTime time = new DateTime(ticks, DateTimeKind.Utc);
                    return (Utility.Settings.OldStyle.Contains("noUtcTime") ? time : time.AddMinutes(this.UtcOffsetMinutes));
                }
            }
            else if (o is JavaScriptString)
            {
                DateTime time3;
                string str2 = o.ToString();
                if (str2.StartsWith("/Date(") && str2.EndsWith(")/"))
                {
                    DateTime time2 = new DateTime(0x7b2, 1, 1, 0, 0, 0, 0, new GregorianCalendar(), DateTimeKind.Utc);
                    time5 = new DateTime(long.Parse(str2.Substring(6, (str2.Length - 6) - 2)) * 0x2710L, DateTimeKind.Utc);
                    return time5.AddTicks(time2.Ticks);
                }
                if (DateTime.TryParseExact(o.ToString(), DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out time3))
                {
                    return (Utility.Settings.OldStyle.Contains("noUtcTime") ? time3 : time3.AddMinutes(this.UtcOffsetMinutes));
                }
            }
            if (obj2 == null)
            {
                throw new NotSupportedException();
            }
            int year = (int) JavaScriptDeserializer.Deserialize(obj2["Year"], typeof(int));
            int month = (int) JavaScriptDeserializer.Deserialize(obj2["Month"], typeof(int));
            int day = (int) JavaScriptDeserializer.Deserialize(obj2["Day"], typeof(int));
            int hour = (int) JavaScriptDeserializer.Deserialize(obj2["Hour"], typeof(int));
            int minute = (int) JavaScriptDeserializer.Deserialize(obj2["Minute"], typeof(int));
            int second = (int) JavaScriptDeserializer.Deserialize(obj2["Second"], typeof(int));
            int millisecond = (int) JavaScriptDeserializer.Deserialize(obj2["Millisecond"], typeof(int));
            DateTime time4 = new DateTime(year, month, day, hour, minute, second, millisecond);
            return (Utility.Settings.OldStyle.Contains("noUtcTime") ? time4 : time4.AddMinutes(this.UtcOffsetMinutes));
        }

        public override string Serialize(object o)
        {
            StringBuilder sb = new StringBuilder();
            this.Serialize(o, sb);
            return sb.ToString();
        }

        public override void Serialize(object o, StringBuilder sb)
        {
            if (!(o is DateTime))
            {
                throw new NotSupportedException();
            }
            DateTime time = (DateTime) o;
            if (!Utility.Settings.OldStyle.Contains("noUtcTime"))
            {
                time = time.ToUniversalTime();
            }
            if (!Utility.Settings.OldStyle.Contains("renderNotASPAJAXDateTime"))
            {
                DateTime time2 = new DateTime(0x7b2, 1, 1, 0, 0, 0, 0, new GregorianCalendar(), DateTimeKind.Utc);
                sb.Append("\"\\/Date(" + ((time.Ticks - time2.Ticks) / 0x2710L) + ")\\/\"");
            }
            else
            {
                JavaScriptUtil.QuoteString(time.ToString(DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb);
            }
        }
    }
}

